#include "osgdatamanager.h"
#include "../GlobalDefinition.h"
#include <osgDB/ReadFile>

namespace HIM {

Q_GLOBAL_STATIC(OsgDataManager, osgDataManager)

OsgDataManager*OsgDataManager::instance()
{
    return osgDataManager();
}

void OsgDataManager::init()
{    
    createBaseNode(Base300);
    createSliceNode(SliceData);

    osg::ref_ptr<osg::Node> model_shell=osgDB::readNodeFile("../data/model/HIM300shell.ply");
    if(model_shell)
        createModelNode(Shell,model_shell);

    osg::ref_ptr<osg::Node> model_Scraper=osgDB::readNodeFile("../data/model/HIM300Scraper.ply");
    if(model_Scraper)
        createModelNode(Scraper,model_Scraper);

    osg::ref_ptr<osg::Node> model_left=osgDB::readNodeFile("../data/model/HIM300left.ply");
    if(model_left)
        createModelNode(Left,model_left);

    osg::ref_ptr<osg::Node> model_core=osgDB::readNodeFile("../data/model/HIM300core.ply");
    if(model_core)
        createModelNode(Core,model_core);

    osg::ref_ptr<osg::Node> model_right=osgDB::readNodeFile("../data/model/HIM300right.ply");
    if(model_right)
        createModelNode(Right,model_right);


}

void OsgDataManager::loadConfigData()
{



}

void OsgDataManager::createBaseNode(QString name)
{
    if(nodeVector.count(name)==0){
        baseNodeMap[name] = QSharedPointer<BaseNode>(new BaseNode());
        nodeVector.push_back(name);
    }
}

void OsgDataManager::createSliceNode(QString name)
{
    if(nodeVector.count(name)==0){
        sliceNodeMap[name] = QSharedPointer<SliceNode>(new SliceNode());
        nodeVector.push_back(name);
    }
}

void OsgDataManager::createModelNode(QString name , osg::Node * model )
{
    if(nodeVector.count(name)==0){
        modelNodeMap[name] = QSharedPointer<ModelNode>(new ModelNode(model));
        nodeVector.push_back(name);
    }
}


BaseNode* OsgDataManager::getBaseNode(QString name)
{
    if(nodeVector.count(name)!=0)
        return  baseNodeMap[name].get();
    else
        return  nullptr;
}


SliceNode* OsgDataManager::getSliceNode(QString name)
{
    if(nodeVector.count(name)!=0)
        return sliceNodeMap[name].get();
    else
        return  nullptr;
}

ModelNode* OsgDataManager::getModelNode(QString name)
{
    if(nodeVector.count(name)!=0)
        return  modelNodeMap[name].get();
    else
        return  nullptr;
}

}
